package com.jy.adscm.operation.mscyb.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.base.bean.JyCustomerProvider;
import com.jy.adscm.operation.base.bean.JyGoods;
import com.jy.adscm.operation.base.bean.JyProviderGoods;
import com.jy.adscm.operation.base.mapper.mysql.JyCustomerProviderMapper;
import com.jy.adscm.operation.base.mapper.mysql.JyGoodsMapper;
import com.jy.adscm.operation.base.mapper.mysql.JyProviderGoodsMapper;
import com.jy.adscm.operation.cg.bean.CgBom;
import com.jy.adscm.operation.cg.bean.CgBomSupplier;
import com.jy.adscm.operation.cg.bean.CgMaterial;
import com.jy.adscm.operation.cg.bean.CgSupplier;
import com.jy.adscm.operation.cg.mapper.mysql.CgBomMapper;
import com.jy.adscm.operation.cg.mapper.mysql.CgBomSupplierMapper;
import com.jy.adscm.operation.cg.mapper.mysql.CgMaterialMapper;
import com.jy.adscm.operation.cg.mapper.mysql.CgSupplierMapper;
import com.jy.adscm.operation.mscyb.bean.JqylMaterial;
import com.jy.adscm.operation.mscyb.bean.QueryJqylMaterial;
import com.jy.adscm.operation.mscyb.mapper.mysql.JqylMaterialMapper;
import com.jy.adscm.operation.mscyb.service.JqylMaterialService;
import com.jy.adscm.operation.purchase.bean.Bomandsupplier;
import com.jy.adscm.operation.purchase.bean.Purchasebom;
import com.jy.adscm.operation.purchase.bean.Supplier;
import com.jy.adscm.operation.purchase.mapper.mysql.BomandsupplierMapper;
import com.jy.adscm.operation.purchase.mapper.mysql.SupplierMapper;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2018-11-29
 */
@Service
public class JqylMaterialServiceImpl extends ServiceImpl<JqylMaterialMapper, JqylMaterial> implements JqylMaterialService {
    @Autowired
    JyUserMapper jyUserMapper;
    @Autowired
    JyCustomerProviderMapper jyCustomerProviderMapper;
    @Autowired
    JyProviderGoodsMapper jyProviderGoodsMapper;
 @Autowired
    CgMaterialMapper cgMaterialMapper;
    @Autowired
    CgBomSupplierMapper cgBomSupplierMapper;
    @Autowired
    CgSupplierMapper cgSupplierMapper;

    @Override
    public List<JyUser> selectTaskPerformPerson() {
        List<JyUser> jyUsers = jyUserMapper.selectTaskPerformPerson("95");
        return jyUsers;
    }

    @Override
    public Boolean updateJqylMaterialy(JqylMaterial jqylMaterial, Token token) {
        //待办修改
        if (jqylMaterial.getTaskstate()==0 && jqylMaterial.getTaskfinishstate()==0 &&
                "0".equals(jqylMaterial.getIstaskstart()) &&  "0".equals(jqylMaterial.getIstaskbegin()) &&
                "0".equals(jqylMaterial.getIstaskend())  && "0".equals(jqylMaterial.getIsreviewtask())
        ){

            return  super.updateById(jqylMaterial);
        }
        //接收任务
        if (jqylMaterial.getTaskstate()==0 && jqylMaterial.getTaskfinishstate()==0 &&
                "1".equals(jqylMaterial.getIstaskstart()) &&  "0".equals(jqylMaterial.getIstaskbegin()) &&
                "0".equals(jqylMaterial.getIstaskend())  && "0".equals(jqylMaterial.getIsreviewtask())
        ){
            jqylMaterial.setIstaskstart("1");
            jqylMaterial.setTaskstartaddtime(new Date());
            jqylMaterial.setTaskstate(1);
            return  super.updateById(jqylMaterial);
        }
        //开始任务
        if (jqylMaterial.getTaskstate()==1 && jqylMaterial.getTaskfinishstate()==0 &&
                "1".equals(jqylMaterial.getIstaskstart()) &&  "1".equals(jqylMaterial.getIstaskbegin()) &&
                "0".equals(jqylMaterial.getIstaskend())  && "0".equals(jqylMaterial.getIsreviewtask())
        ){
            jqylMaterial.setIstaskbegin("1");
            jqylMaterial.setTaskbeginaddtime(new Date());
            jqylMaterial.setRemainingnums( Float.valueOf(String.valueOf(jqylMaterial.getActualcompletenums())) );
            return super.updateById(jqylMaterial);
        }
        //结束任务
        if (jqylMaterial.getTaskstate()==1 && jqylMaterial.getTaskfinishstate()==0 &&
                "1".equals(jqylMaterial.getIstaskstart()) &&  "1".equals(jqylMaterial.getIstaskbegin()) &&
                "1".equals(jqylMaterial.getIstaskend())  && "0".equals(jqylMaterial.getIsreviewtask())
        ){
            jqylMaterial.setIstaskend("1");
            jqylMaterial.setTaskfinishstate(1);
            jqylMaterial.setTaskendaddtime(new Date());
            return super.updateById(jqylMaterial);
        }
        //复核任务
        if (jqylMaterial.getTaskstate()==1 && jqylMaterial.getTaskfinishstate()==1 &&
                "1".equals(jqylMaterial.getIstaskstart()) &&  "1".equals(jqylMaterial.getIstaskbegin()) &&
                "1".equals(jqylMaterial.getIstaskend())  && "1".equals(jqylMaterial.getIsreviewtask())
        ){
            jqylMaterial.setIsreviewtask("1");
            jqylMaterial.setReviewaddtime(new Date());
            jqylMaterial.setReviewperson(token.getUser().getId().toString());
            return super.updateById(jqylMaterial);
        }

        return null;
    }

    @Override
    public Page<JqylMaterial> selectJqylMaterial(Page<JqylMaterial> page, QueryJqylMaterial queryJqylMaterial) {

        Wrapper<JqylMaterial> wrapper =new EntityWrapper<>();
        wrapper.eq("TASKSTATE",queryJqylMaterial.getTaskState());
        wrapper.eq("TASKFINISHSTATE",queryJqylMaterial.getTaskFinishState());
        wrapper.eq("isReviewTask",queryJqylMaterial.getIsReviewTask());
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryJqylMaterial.getBom(),null)){
            wrapper.eq("RESOURCENAME",queryJqylMaterial.getBom());
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryJqylMaterial.getSupplier(),null)){
            wrapper.eq("BUSINESSTYPE",queryJqylMaterial.getSupplier());
        }
        page =super.selectPage(page, wrapper);
        List<JqylMaterial> jqylMaterials = page.getRecords();
        for (JqylMaterial jqylMaterial : jqylMaterials) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(jqylMaterial.getResourcename(),null)){
                CgMaterial cgMaterial = cgMaterialMapper.selectById(jqylMaterial.getResourcename());
                if (AdscmBaseUtils.getInstance().isNotEmpty(cgMaterial,null)){
                    jqylMaterial.setResourcenameName(cgMaterial.getName());
                }else {
                    jqylMaterial.setResourcenameName(jqylMaterial.getResourcename());
                }
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(jqylMaterial.getBusinesstype(),null)){
                CgSupplier cgSupplier = cgSupplierMapper.selectById(jqylMaterial.getBusinesstype());
                jqylMaterial.setBusinesstypeName(cgSupplier.getName());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(jqylMaterial.getTaskreleaseperson(),null)){
                JyUser jyUser = jyUserMapper.selectById(jqylMaterial.getTaskreleaseperson());
                jqylMaterial.setTaskreleasepersonName(jyUser.getName());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(jqylMaterial.getTaskperformperson(),null)){
                JyUser jyUser = jyUserMapper.selectById(jqylMaterial.getTaskperformperson());
                jqylMaterial.setTaskperformpersonName(jyUser.getName());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(jqylMaterial.getReviewperson(),null)){
                JyUser jyUser = jyUserMapper.selectById(jqylMaterial.getReviewperson());
                jqylMaterial.setReviewpersonName(jyUser.getName());
            }

            DecimalFormat df=new DecimalFormat("#0.0000");
            String format = df.format(jqylMaterial.getRemainingnums());
            jqylMaterial.setRemainingnums(Float.valueOf(format));
        }
        page.setTotal(super.selectCount(wrapper));

        return page;
    }

    @Override
    public List<CgMaterial> selectJqylMaterialGoods() {
        List<CgMaterial> cgMaterials = cgMaterialMapper.selectList(null);
        return cgMaterials;
    }

    @Override
    public List<CgSupplier> selectJqylMaterialProvider(String goodsId) {
        List<CgSupplier> jyCustomerProviders= new ArrayList<>();
        Wrapper<CgBomSupplier> wrapper=new EntityWrapper<>();
        wrapper.eq("bid",goodsId);
        List<CgBomSupplier> cgBomSuppliers = cgBomSupplierMapper.selectList(wrapper);
        for (CgBomSupplier cgBomSupplier : cgBomSuppliers) {
            Long sid = cgBomSupplier.getSid();
            CgSupplier cgSupplier = cgSupplierMapper.selectById(sid);
            jyCustomerProviders.add(cgSupplier);
        }
        return jyCustomerProviders;
    }

    @Override
    public JqylMaterial updateJqylMaterialyValue(String id, Float value) throws AdscmSystemException{
        //只能有一个线程进入修改库存
        synchronized (this){
            JqylMaterial jqylMaterial = super.selectById(id);
            if (!AdscmBaseUtils.getInstance().isNotEmpty(jqylMaterial, null)) {
                throw new AdscmSystemException("未查询到该物料");
            }
            if (!"1".equals(jqylMaterial.getIstaskbegin())) {
                throw new AdscmSystemException("物料未入库");
            }
            if (jqylMaterial.getRemainingnums() == 0) {
               // throw new AdscmSystemException("物料已经用完");
            }
            if ((jqylMaterial.getRemainingnums()+value)<0) {
                throw new AdscmSystemException("使用量超过了剩余量");
            }
            jqylMaterial.setRemainingnums(jqylMaterial.getRemainingnums()+value);
            if ( jqylMaterial.getRemainingnums()==0){
                jqylMaterial.setIstaskend("0");
            }
             super.updateById(jqylMaterial);
            return jqylMaterial;
        }
    }

    @Override
    public boolean insertJqylMaterial(JqylMaterial jqylMaterial) {
        CgMaterial cgMaterial = cgMaterialMapper.selectById(jqylMaterial.getResourcename());
        jqylMaterial.setTypenums(cgMaterial.getType());
        boolean insert = super.insert(jqylMaterial);
        return insert;
    }


}
